import { login, getInfo } from '@/api/user'
import { auth } from '@/utils/auth' 
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: auth.getToken(),
    name: '',
    uid: auth.getUid(),
    phone: '',
    avatar: '',
    permissions: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_UID: (state, uid) => {
    state.uid = uid
  },
  SET_PHONE: (state, phone) => {
    state.phone = phone
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { phone, code } = userInfo
    return new Promise((resolve, reject) => {
      login({ phone: phone, code: code }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.accessToken)
        auth.setUid(data.user.id)
        auth.setToken(data.accessToken)
        resolve(data.user.id)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit }) {
    return new Promise((resolve, reject) => {
      getInfo(auth.getUid()).then(response => {
        const { data } = response
        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { name, id, phone } = data
        let pIds = [...new Set(data.permissionIds)];
        const pList = ['project','surveyList','surveyProcess','schemeList','schemeProcess','jobTicketList','jobTicketProcess','videoSupervision', 'userDepartment']
        let permissions = []
        pList.forEach((item, index)=> {
          if(pIds.indexOf(index + 1) != -1) permissions.push(item)
        })
        // let permissions = ['/ticket','list','approve','/video','playBack','realTime', 'liveTest','/setting','user']
        if (!permissions || permissions.length <= 0) {
          reject('getInfo: permissions must be a non-null array!')
        }
        data.permissions = permissions

        commit('SET_PERMISSIONS', permissions)
        commit('SET_UID', id)
        commit('SET_NAME', name)
        commit('SET_PHONE', phone)
        commit('SET_AVATAR', '')
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      // logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_PERMISSIONS', [])
        auth.removeToken() // must remove  token  first
        resetRouter()
        resolve()
      // }).catch(error => {
      //   reject(error)
      // })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_PERMISSIONS', [])
      auth.removeToken() // must remove  token  first
      auth.removeUid()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

